from pyjsparser import parse
import ast
 
def analyze_angularjs(file_path):
    with open(file_path) as f:
        code = f.read()
    
    # 解析JS语法树
    parsed = parse(code)
    
    # 提取控制器信息
    controllers = []
    for node in parsed['body']:
        if node['type'] == 'ExpressionStatement' and \
           node['expression']['callee']['property']['name'] == 'controller':
            ctrl = {
                'name': node['expression']['arguments'][0]['value'],
                'dependencies': [],
                'methods': []
            }
            # 解析依赖注入
            for arg in node['expression']['arguments'][1]['params']:
                ctrl['dependencies'].append(arg['name'])
            # 解析函数逻辑
            traverse_controller(node['expression']['arguments'][1]['body'], ctrl)
            controllers.append(ctrl)
    
    return controllers
 
def traverse_controller(node, ctrl):
    if node['type'] == 'BlockStatement':
        for stmt in node['body']:
            if stmt['type'] == 'ExpressionStatement' and \
               stmt['expression']['left']['object']['name'] == '$scope':
                method_name = stmt['expression']['left']['property']['name']
                ctrl['methods'].append({
                    'name': method_name,
                    'type': 'function' if stmt['expression']['right']['type'] == 'FunctionExpression' 
                            else 'property'
                })

result = analyze_angularjs('/Users/justinzhang/WorkSpace/com.ein/project/南京/angularjs/platform-ui/html/bms/js/customerCare/customerInfoController.js')
print(result)
